export const dateFormat = function (date, format) {
  const now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var date = now.getDate();
  if (month <= 9) {
    month = `0${month}`;
  }
  if (date <= 9) {
    date = `0${date}`;
  }
  if (format !== undefined && format !== undefined) {
    return format
      .replace("yyyy", year)
      .replace("MM", month)
      .replace("dd", date);
  }
  return `${year}-${month}-${date}`;
};

export const today = function (format) {
  return dateFormat(new Date(), format);
};

export const arrayKeys = function (array, key) {
  const keys = [];
  for (var i = 0; i < array.length; i++) {
    const each = array[i];
    const value = each[key];
    if (value !== undefined && value !== null && !keys.includes(value)) {
      keys.push(value);
    }
  }
  return keys;
};

export const array2Matrix = function (array, rowKey, columnKey, valueKey) {
  const rows = [];
  const columns = [];
  for (var i = 0; i < array.length; i++) {
    const each = array[i];
    const row = each[rowKey];
    if (row !== undefined && row !== null && !rows.includes(row)) {
      rows.push(row);
    }
    const column = each[columnKey];
    if (column !== undefined && column !== null && !columns.includes(column)) {
      columns.push(column);
    }
  }
  const matrix = [rows.length][columns.length];
  for (var i = 0; i < array.length; i++) {
    const each = array[i];
    const row = each[rowKey];
    const column = each[columnKey];
    const value = each[valueKey];
    const rowIndex = rows.indexOf(row);
    const columnIndex = columns.indexOf(column);
    if (rowIndex >= 0 && columnIndex >= 0) {
      matrix[rowIndex][columnIndex] = value;
    }
  }
  return matrix;
};

export const array2DoubleGroup = function (array, rowKey, columnKey, valueKey) {
  const rowMap = {};
  for (var i = 0; i < array.length; i++) {
    const each = array[i];
    const rowValue = each[rowKey];
    const columnValue = each[columnKey];
    const value = each[valueKey];
    if (rowMap[rowValue] === undefined) {
      rowMap[rowValue] = {};
      rowMap[rowValue][rowKey] = rowValue;
    }
    rowMap[rowValue][columnValue] = value;
  }
  const keys = Object.keys(rowMap);
  const group = [];
  for (var i = 0; i < keys.length; i++) {
    group.push(rowMap[keys[i]]);
  }
  return group;
};

export const array2ArrayMap = function (array, groupKey, xAxisKey, valueKey) {
  const arrayMap = {};
  for (var i = 0; i < array.length; i++) {
    const item = array[i];
    const group = item[groupKey];
    const xAxis = item[xAxisKey];
    const value = item[valueKey];
    if (arrayMap[group] === undefined) {
      arrayMap[group] = [];
    }
    arrayMap[group].push([xAxis, value]);
  }
  return arrayMap;
};

export const splitToMultiArray = function (array, groupKey) {
  const arrayMap = {};
  for (var i = 0; i < array.length; i++) {
    const each = array[i];
    const groupValue = each[groupKey];
    if (arrayMap[groupValue] === undefined) {
      arrayMap[groupValue] = [];
    }
    arrayMap[groupValue].push(each);
  }
  const keys = Object.keys(arrayMap);
  const multiArray = [];
  for (var i = 0; i < keys.length; i++) {
    multiArray.push(arrayMap[keys[i]]);
  }
  return multiArray;
};

export default {
  today,
  arrayKeys,
  array2Matrix,
  array2DoubleGroup,
  splitToMultiArray,
};
